package com.vfa.fishing.data;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class PersonModel {
    private DBAdapter myDAdapter;

    public PersonModel(Context context) {
        myDAdapter = new DBAdapter(context);
        create();
    }

    private void create() {
        try {
            myDAdapter.createDataBase();
        } catch (IOException ioe) {
            Log.e("LocNNT ",
                    "TbDiet   Unable to create database  =============");
        }
        try {
            myDAdapter.openDataBase();
        } catch (SQLException sqle) {
            Log.e("LocNNT ", "TbDiet   Unable to Open database  =============");
        }

    }

    public void Insert(int xfishing, int yfishing, int direction,
            int sideAppear, int blood, int strength, int lengthsOfline,
            int amoutnOFMoney, int iscatchOk, int ismoving, int istimedelay,
            int Xposition, int Yposition) { // , boolean isMoving,boolean
                                            // isTimeDelayInFishing) {
        if (!myDAdapter.myDataBase.isOpen()) {
            myDAdapter.openDataBase();
        }
        String sql = "INSERT INTO tbl_person(xFishing,yFishing, direction, sideAppear, blood, strength, lengthsOfline, amoutnOFMoney,isCatchOk,isMoving,isTimeDelayInFishing,Xposition, Yposition) VALUES ("
                + xfishing
                + ",'"
                + yfishing
                + "','"
                + direction
                + "','"
                + sideAppear
                + "','"
                + blood
                + "','"
                + strength
                + "','"
                + lengthsOfline
                + "','"
                + amoutnOFMoney
                + "','"
                + iscatchOk
                + "','"
                + ismoving
                + "','"
                + istimedelay
                + "','"
                + Xposition
                + "','" + Yposition + "')";
        myDAdapter.execSQL(sql);
    }

    public ArrayList<PersonToRestore>getDataList() {

        if (!myDAdapter.myDataBase.isOpen()) {
            myDAdapter.openDataBase();
        }
//        PersonToRestore lst = new PersonToRestore();
        ArrayList<PersonToRestore> lst = new ArrayList<PersonToRestore>();
        Cursor result = null;
        String sql = "SELECT * FROM tbl_person ";

        result = myDAdapter.Query(sql);
        if (result == null) {
            myDAdapter.close();
            return null;
        }
        if (result.moveToFirst()) {
            do {
                try {

                    PersonToRestore objDB = new PersonToRestore();
                    objDB.setXFishing(result.getInt(0));
                    objDB.setYFishing(result.getInt(1));
                    objDB.setDirection(result.getInt(2));
                    objDB.setSideAppear(result.getInt(3));
                    objDB.setBlood(result.getInt(4));
                    objDB.setStrength(result.getInt(5));
                    objDB.setLengthsOfline(result.getInt(6));
                    objDB.setAmoutnOFMoney(result.getInt(7));
                    objDB.setIsCatchOk(result.getInt(8));
                    objDB.setMoving(result.getInt(9));
                    objDB.setIsTimeDelayInFishing(result.getInt(10));
                    objDB.setXposition(result.getInt(11));
                    objDB.setYposition(result.getInt(12));
                    lst.add(objDB);
                } catch (SQLiteException ex) {
                    Log
                            .d("Data", "Error when load recipe data"
                                    + ex.toString());
                }

            } while (result.moveToNext());
        }

        if (result != null && !result.isClosed()) {
            result.close();
        }

        myDAdapter.close();
        return lst;
    }

    public void deleteAll() {
        if (!myDAdapter.myDataBase.isOpen()) {
            myDAdapter.openDataBase();
        }
        String DELETE = "delete from tbl_person";
        myDAdapter.myDataBase.execSQL(DELETE);
    }

}
